#include <iostream>
#include <string>
using namespace std;

const int N = 100005;
int q[N], hh, tt = -1;

int main()
{
    int m; cin >> m;
    while(m--)
    {
        string str; cin >> str;
        if(str == "push") cin >> q[++tt];
        if(str == "pop") ++hh;
        if(str == "empty") cout << (hh > tt ? "YES\n" : "NO\n");
        if(str == "query") cout << q[hh] << endl;
    }
    return 0;
}